package activity_atividades;

import java.sql.Date;
import java.text.SimpleDateFormat;

import util.TableHeaderTextView;
import util.TableRowTextView;
import activity_alarmes.Alarmes;
import activity_disciplinas.Disciplinas;
import activity_provas.Provas;
import activity_trabalhos.Trabalhos;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.Time;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.adjt.droidsapiens.MainHome;
import com.adjt.droidsapiens.R;

import database.DatabaseConnector;

@SuppressLint("SimpleDateFormat")
public class Atividades extends Activity {
	
	public static final int NOVA_ATIVIDADE = 0;	

	Intent it;
	int semestreAtual;
	DatabaseConnector db;
	boolean buscaAtividadesAntigas;
	public static final int DISCIPLINAS = 0;
	public static final int PROVAS = 1;
	public static final int TRABALHOS = 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_atividades);
		
		semestreAtual = getIntent().getIntExtra("semestreAtual", 0);
		buscaAtividadesAntigas = getIntent().getBooleanExtra("buscaAtividadesAtigas", false);
		
		Time time = new Time(Time.getCurrentTimezone());
		time.normalize(true);
		time.setToNow();
		long now = time.toMillis(true);
		String sData, sHora;
		SimpleDateFormat data = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat hora = new SimpleDateFormat("HH:mm");
		
		db = new DatabaseConnector(getBaseContext());
		Cursor c;
		db.open();
		String tables = "Atividade";
		String[] column = new String[]{"idAtividade", "nome", "data", "local"};
		String where = "tipo = 0 AND ";
		if(!buscaAtividadesAntigas) {
			where += "data > "+ now;
		}else
			where +="data < "+ now;
		String orderBy = "data";
		
		TableLayout layout = (TableLayout) findViewById(R.id.Atividades_TableLayout);
		
		Resources r = getResources();
		int pxw = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 220, r.getDisplayMetrics());
		int pxb = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
		int pxz = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, r.getDisplayMetrics());
		
		c = db.buscar(tables, column, where, orderBy);
		String temp;
		TableRow tr;
		long miliseg;
		if (c.getCount() > 0) {
			while(c.moveToNext()){
				temp = c.getString(c.getColumnIndex("nome"));
				if(temp.length() > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER);
					
					TableHeaderTextView labelNome = new TableHeaderTextView(this);
					labelNome.setText(R.string.nome);
					labelNome.setWidth(pxz);
					tr.addView(labelNome);
					TableRowTextView nomeAtividadeTxt = new TableRowTextView(this); 
					nomeAtividadeTxt.setText(temp);
					nomeAtividadeTxt.setWidth(pxw);
					tr.addView(nomeAtividadeTxt);
					layout.addView(tr);
				}
				
				temp = c.getString(c.getColumnIndex("local"));
				if(temp.length() > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER);
					
					TableHeaderTextView labelLocal = new TableHeaderTextView(this);
					labelLocal.setText(R.string.local);
					labelLocal.setWidth(pxz);
					tr.addView(labelLocal);
					TableRowTextView localAtividadeTxt = new TableRowTextView(this); 
					localAtividadeTxt.setText(temp);
					localAtividadeTxt.setWidth(pxw);
					tr.addView(localAtividadeTxt);
					layout.addView(tr);
				}
				
				miliseg = c.getLong(c.getColumnIndex("data"));
				if(miliseg > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER);
					
					TableHeaderTextView labelData = new TableHeaderTextView(this);
					labelData.setText(R.string.data);
					labelData.setWidth(pxz);
					tr.addView(labelData);
					TableRowTextView dataAtividadeTxt = new TableRowTextView(this);
					miliseg = c.getLong(c.getColumnIndex("data"));
					dataAtividadeTxt.setWidth(pxw);
					sData = data.format(new Date(miliseg));
					dataAtividadeTxt.setText(sData);
					tr.addView(dataAtividadeTxt);
					layout.addView(tr);
				}
				
				if(miliseg > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER);
					
					TableHeaderTextView labelHora = new TableHeaderTextView(this);
					labelHora.setText(R.string.hora);
					labelHora.setWidth(pxz);
					tr.addView(labelHora);
					TableRowTextView horaAtividadeTxt = new TableRowTextView(this);
					sHora = hora.format(new Date(miliseg));
					horaAtividadeTxt.setText(sHora);
					horaAtividadeTxt.setWidth(pxw);
					tr.addView(horaAtividadeTxt);
					layout.addView(tr);
				}
				
				LinearLayout ll = new LinearLayout(this);
				ll.setGravity(Gravity.CENTER);
				
				Button verDisciplina = new Button(this);
				verDisciplina.setTag(c.getString(c.getColumnIndex("idAtividade")));
				verDisciplina.setBackgroundResource(R.drawable.ic_view);
				verDisciplina.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						it = new Intent(getBaseContext(), ExibirAtividade.class);
						it.putExtra("idAtividade", Integer.parseInt(v.getTag().toString()));
						it.putExtra("semestreAtual", semestreAtual);
						startActivity(it);
						finish();
					}
				});
				verDisciplina.setHeight(pxb);
				verDisciplina.setWidth(pxb);
				ll.addView(verDisciplina);
				
				Button addAlarme = new Button(this);
				addAlarme.setTag(c.getString(c.getColumnIndex("idAtividade")));
				addAlarme.setBackgroundResource(R.drawable.ic_alarm);
				addAlarme.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						it = new Intent(getBaseContext(), Alarmes.class);
						it.putExtra("idAtividade", Integer.parseInt(v.getTag().toString()));
						it.putExtra("semestreAtual", semestreAtual);
						startActivity(it);
						finish();
					}
				});
				addAlarme.setHeight(pxb);
				addAlarme.setWidth(pxb);
				ll.addView(addAlarme);
				layout.addView(ll);
				
				tr = new TableRow(this);
				TextView vazia = new TextView(this);
				tr.addView(vazia);
				layout.addView(tr);
			}
		}
		else {
			tr = new TableRow(this);
			tr.setGravity(Gravity.CENTER);
			
			TextView listaVazia = new TextView(this);
			listaVazia.setText(R.string.listaAtividadesVazia);
			tr.addView(listaVazia);
			
			layout.addView(tr);
		}
		db.close();
	}
	
	public void goNovaAtividade(View v){
		Intent it = new Intent(getBaseContext(), NovaAtividade.class);
		it.putExtra("semestreAtual", semestreAtual);
		startActivity(it);
		finish();
	}
	
	@Override
	public void onBackPressed(){
		it = new Intent(getBaseContext(), MainHome.class);
		it.putExtra("semestreAtual", semestreAtual);
		startActivity(it);
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, DISCIPLINAS, 1, R.string.disciplinas);
		menu.add(0, PROVAS, 2, R.string.provas);
		menu.add(0, TRABALHOS, 3, R.string.trabalhos);
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featuredId, MenuItem item){
		
		switch(item.getItemId()){
		
		case DISCIPLINAS:
			it = new Intent(getBaseContext(), Disciplinas.class);
			it.putExtra("semestreAtual", semestreAtual);
			startActivity(it);
			finish();
			return true;
		  
		case PROVAS:
			it = new Intent(getBaseContext(), Provas.class);
			it.putExtra("semestreAtual", semestreAtual);
			startActivity(it);
			finish();
			return true;
			  
		case TRABALHOS:
			Intent it = new Intent(getBaseContext(), Trabalhos.class);
			it.putExtra("semestreAtual", semestreAtual);
			startActivity(it);
			finish();
			return true;	  
		}
		return false;
		
	}

}
